<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <link rel="stylesheet" href="/static/font/iconfont.css">

</head>
<body>
<div style="margin:40px;" class="layui-form">
    <input id="touristAttraction" type="hidden" value="<$ touristAttraction $>">
    <input id="attend_record" type="hidden" value="<$ attend_record $>">
    <input id="build_id" type="hidden" value="<$ build_id $>">
    <div class="layui-form-item">
        <label class="layui-form-label">补签签到点</label>
        <div class="layui-input-block">
            <select name="interest" lay-filter="aihao" id="view">

            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">补签时间</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" id="test4" placeholder="HH:mm:ss">
        </div>
    </div>
    <div class="layui-form-item" style="margin-top: 40px">
        <div class="layui-input-block">
            <button class="layui-btn" id="confirm" onclick="confirmData()">确定</button>
            <button type="reset" class="layui-btn layui-btn-primary" onclick="closeHtml()">取消</button>
        </div>
    </div>
</div>
</body>

<!--[if lt IE 9]>
<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<script src="/static/layui/layui.js"></script>
<script>
  var $, layer, postData;

  function compare(s1, s2) {
    var reDate = /\d{4}-\d{1,2}-\d{1,2} /;
    s1 = new Date((reDate.test(s1) ? s1 : '2017-1-1 ' + s1).replace(/-/g, '/'));
    s2 = new Date((reDate.test(s2) ? s2 : '2017-1-1 ' + s2).replace(/-/g, '/'));
    var ms = s2.getTime() - s1.getTime();
    if (ms < 0) return false;
    return true;
  }

  function confirmData() {
    var attraction = $('#view').val();
    var time = $('#test4').val();

    if (!time) {
      layer.alert('请先选择签到时间');
      return;
    }

    var startTime = null;
    var endTime = null;

    for (var j = 0; j < record.length; j++) {
      if (record[j].tourist_attraction === startCode) {
        startTime = record[j].attend_time.substring(11);
      }
      if (record[j].tourist_attraction === endCode) {
        endTime = record[j].attend_time.substring(11);
      }
    }

    if (startTime && compare(time, startTime)) {
      layer.alert('签到时间需大于入园时间');
      return
    }

    if (endTime && compare(endTime, time)) {
      layer.alert('签到时间需小于出园时间');
      return
    }

    var data = {
      build_id: buildId,
      attraction: attraction,
      time: time
    };
    editSign(data);
  }

  function editSign(data) {
    $.get('/report/reSign?x-tm-token=' + localStorage.tmToken, data, function (response) {
      if (response.errno == 0) {
        closeHtml();
      } else {
        layer.alert('补签失败 ' + response.errmsg);
      }
    });
  }

  function closeHtml() {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index);
    parent.window.location.reload();
  }

  var arr, record, startCode, endCode, buildId;
  layui.use(['layer', 'form', 'laydate'], function () {
    $ = layui.$;
    layer = layui.layer;
    var laydate = layui.laydate;

    //执行一个laydate实例
    laydate.render({
      elem: '#test4'
      , type: 'time'
    });

    var htmlStr = '';

    arr = JSON.parse($("#touristAttraction").val());
    record = JSON.parse($("#attend_record").val());
    buildId = JSON.parse($("#build_id").val());


    for (var i = 0; i < arr.length; i++) {
      if (arr[i].status == 2) {
        startCode = arr[i].type_code;
      }
      if (arr[i].status == 3) {
        endCode = arr[i].type_code;
      }
      for (var j = 0; j < record.length; j++) {
        if (record[j].tourist_attraction === arr[i].type_code) {
          break;
        }
        if (j === record.length - 1) {
          htmlStr += '<option value="' + arr[i].type_code + '">' + arr[i].name + '</option>';
        }
      }
      if(record.length===0){
        htmlStr += '<option value="' + arr[i].type_code + '">' + arr[i].name + '</option>';
      }
    }
    $('#view').html(htmlStr);
    var form = layui.form;
    form.render();
    if (postData) {
      $('#major').val(postData.major);
      $('#minor').val(postData.minor);
    }
  });

  // 获取URL参数 e.g. map.html?sid=122134
  function getUrlParam(s, name) {
    var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
    if (reg.test(s))
      return unescape(RegExp.$2.replace(/\+/g, " "));
    return "";
  }

</script>
